﻿using log4net;
using McsUtility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MinerManager.Forms
{
	public partial class UserChangeStaff : Form
	{
		ILog logger = LogManager.GetLogger(typeof(Program));
		private Common.AgentComponent Agent = null;

		public event EventHandler<EventArgs> OnSuccess;

		private List<McsUtility.Models.StaffModel> infosStaff = new List<McsUtility.Models.StaffModel>();

		public UserChangeStaff(Common.AgentComponent agent)
		{
			InitializeComponent();
			this.Agent = agent;
		}

		#region 窗体事件

		private void UserChangeStaff_Load(object sender, EventArgs e)
		{
			this.Agent.OnNodeServerMinerManagerGetStaffs += new EventHandler<ByteEventArgs>(Agent_OnGetRegSmsCodeReceive);
			this.Agent.OnNodeServerMinerManagerReStaff += new EventHandler<ByteEventArgs>(Agent_OnNodeServerMinerManagerReStaff);

			MyStaffs_Name.Text = Common.Data.Staff.Name;
			MyStaffs_QQ.Text = Common.Data.Staff.QQ1;
			MyStaffs_Wechat.Text = Common.Data.Staff.WeiXin1;

			//连接服务器
			if (!this.Agent.IsStarted)
			{
				Common.ConnectionServer conn = new Common.ConnectionServer(this.Agent);
				conn.Connection();
			}

			byte[] bytes = ProtocolHelper.WritePackage(ProtoType.MinerManagerGetStaffs, "");
			this.Agent.AgentSend(bytes);
		}

		private void UserChangeStaff_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.Agent.OnNodeServerMinerManagerGetStaffs -= new EventHandler<ByteEventArgs>(Agent_OnGetRegSmsCodeReceive);
			this.Agent.OnNodeServerMinerManagerReStaff -= new EventHandler<ByteEventArgs>(Agent_OnNodeServerMinerManagerReStaff);
		}

		#endregion

		#region Agent事件

		private void Agent_OnGetRegSmsCodeReceive(object sender, ByteEventArgs e)
		{
			infosStaff = ProtocolHelper.ReadPackageBody<List<McsUtility.Models.StaffModel>>(e.Bytes);

			//获取默认客户经理
			string staffId = System.Configuration.ConfigurationManager.AppSettings["StaffId"];
			if (string.IsNullOrWhiteSpace(staffId))
				staffId = "2";

			//控件操作
			List<ComboboxListItem> items = new List<ComboboxListItem>();
			foreach (McsUtility.Models.StaffModel info in infosStaff)
			{
				items.Add(new ComboboxListItem() { Text = info.Name, Value = info.ID.ToString() });
			}
			Common.Utility.SetComboBox(this, "ItemStaffs", items, Common.Data.Staff.ID > 0 ? Common.Data.Staff.ID.ToString() : staffId);
		}

		private void Agent_OnNodeServerMinerManagerReStaff(object sender, ByteEventArgs e)
		{
			McsUtility.Models.ResultModel result = ProtocolHelper.ReadPackageBody<McsUtility.Models.ResultModel>(e.Bytes);
			if (result.success)
			{
				McsUtility.Models.LoginResultModel info = TomNet.Utility.Data.JsonHelper.FromJson<McsUtility.Models.LoginResultModel>(result.data.ToString());
				Common.Data.Staff = info.Staff;

				OnSuccess?.Invoke(this, new EventArgs());
				Common.Utility.FormClose(this);
			}
			else
			{
				MessageBox.Show("保存失败，请尝试重新操作。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		#endregion

		#region void ItemStaffs_SelectedIndexChanged(object sender, EventArgs e)

		private void ItemStaffs_SelectedIndexChanged(object sender, EventArgs e)
		{
			ItemStaffs_QQ.Text = infosStaff[ItemStaffs.SelectedIndex].QQ1;
			ItemStaffs_Wechat.Text = infosStaff[ItemStaffs.SelectedIndex].WeiXin1;
		}

		#endregion

		#region void BtnSave_Click(object sender, EventArgs e)

		private void BtnSave_Click(object sender, EventArgs e)
		{
			//连接服务器
			if (!this.Agent.IsStarted)
			{
				Common.ConnectionServer conn = new Common.ConnectionServer(this.Agent);
				conn.Connection();
			}

			if (this.Agent.IsStarted)
			{
				McsUtility.Models.RegModel info = new McsUtility.Models.RegModel
				{
					LoginName = Common.Data.LoginUserName,
					LoginPass = "",
					SMSCode = 0,
					Name = "",
					PassQuestion1 = "",
					PassQuestion2 = "",
					PassQuestion3 = ItemStaffs.SelectedValue.ToString(),
					PassAnswer1 = "",
					PassAnswer2 = "",
					PassAnswer3 = "",
					CardID = "",
					Sex = 1
				};
				byte[] bytes = ProtocolHelper.WritePackage<McsUtility.Models.RegModel>(ProtoType.MinerManagerReStaff, info);
				this.Agent.AgentSend(bytes);
			}
			else
			{
				MessageBox.Show("服务器连接已断开，请检查网络是否畅通后重新操作。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		#endregion

		#region void BtnClose_Click(object sender, EventArgs e)

		private void BtnClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		#endregion
	}
}
